//
//  EaseBubbleView+TransmitText.m
//  
//
//  Created by Yunuo on 16/9/23.
//
//

#import "EaseBubbleView+TransmitText.h"
//#import "ToolHelper.h"
@implementation EaseBubbleView (TransmitText)

#pragma mark - private

- (void)_setupTransmitTextBubbleMarginConstraints {
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.tmTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.tmTextView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.tmTextView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.tmTextView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
}
- (void)_setupTransmitTextBubbleConstraints
{
    [self _setupTransmitTextBubbleMarginConstraints];
    //转发内容
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTextLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTextLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTextLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTextLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmFromLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
//    “转发消息来自”
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tmTextLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.lineImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
//    转发消息来自的名字
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromNameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tmFromLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromNameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmFromLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromNameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmFromLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmFromNameLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.tmTextView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
//    线
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lineImg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tmFromLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lineImg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-7]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lineImg attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.lineImg attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
    
//   头像
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.lineImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:-3.5]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.tmTextView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40]];
//   第一个字或字母
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
//    名字
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmNameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmNameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:4]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmNameLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tmTimelabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmNameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmlittleTextLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
//    时间
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTimelabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lineImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTimelabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmNameLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:3]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTimelabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmNameLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmTimelabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
//    小的内容
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmlittleTextLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tmNameLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:1]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmlittleTextLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tmNameLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmlittleTextLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tmlittleTextLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tmTextView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
}

#pragma mark - public
//初始化控件
- (void)setupTransmitTextBubbleView {
    
    self.tmTextView = [[UIImageView alloc] init];
    self.tmTextView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.backgroundImageView addSubview:self.tmTextView];
    //转发内容
    self.tmTextLabel = [[UILabel alloc] init];
    self.tmTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.tmTextLabel.font = systemFont(15);
    self.tmTextLabel.numberOfLines = 0;
    [self.tmTextView addSubview:self.tmTextLabel];
    self.tmTextLabel.text = @"转发内容";
    
    //转发来自谁
    self.tmFromLabel = [[UILabel alloc] init];
    self.tmFromLabel.translatesAutoresizingMaskIntoConstraints = NO;
//    self.tmFromLabel.backgroundColor = [UIColor orangeColor];
    self.tmFromLabel.textColor = [UIColor grayColor];
    self.tmFromLabel.font = systemFont(14);
    self.tmFromLabel.textAlignment = NSTextAlignmentLeft;
    [self.tmTextView addSubview:self.tmFromLabel];
    self.tmFromLabel.text = @"转发消息来自:";
    //转发来自谁
    self.tmFromNameLabel = [[UILabel alloc] init];
    self.tmFromNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.tmFromNameLabel.backgroundColor = [UIColor clearColor];
    self.tmFromNameLabel.textColor = [UIColor grayColor];
    self.tmFromNameLabel.font = systemFont(14);
    self.tmFromNameLabel.textAlignment = NSTextAlignmentLeft;
    [self.tmTextView addSubview:self.tmFromNameLabel];
    self.tmFromNameLabel.text = @"旺仔";
    
    //line
    self.lineImg = [[UIImageView alloc] init];
    self.lineImg.translatesAutoresizingMaskIntoConstraints = NO;
    self.lineImg.image = [UIImage imageNamed:@"HuanXin.bundle/transmittext_line2"];
    [self.tmTextView addSubview:self.lineImg];
    
    //头像40*40
    self.headImg = [[UIView alloc] init];
    self.headImg.translatesAutoresizingMaskIntoConstraints = NO;
    self.headImg.backgroundColor = HXSystemColor;
    self.headImg.layer.cornerRadius = 20;
    self.headImg.clipsToBounds = YES;
    [self.tmTextView addSubview:self.headImg];
    
    //第一个字母或者文字
    self.firstName = [[UILabel alloc] init];
    self.firstName.translatesAutoresizingMaskIntoConstraints= NO;
    self.firstName.textColor = [UIColor whiteColor];
    self.firstName.font = systemFont(19);
    self.firstName.backgroundColor = [UIColor clearColor];
    self.firstName.textAlignment = NSTextAlignmentCenter;
    [self.headImg addSubview:self.firstName];
    self.firstName.text = @"A";
   
    //转发的名字
    self.tmNameLabel = [[UILabel alloc] init];
    self.tmNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
//    self.tmNameLabel.backgroundColor = [UIColor clearColor];
    self.tmNameLabel.textColor = [UIColor grayColor];
    self.tmNameLabel.textAlignment = NSTextAlignmentLeft;
    self.tmNameLabel.font = systemFont(14);
    [self.tmTextView addSubview:self.tmNameLabel];
    self.tmNameLabel.text = @"我的name";
    
    //转发时间
    self.tmTimelabel = [[UILabel alloc] init];
    self.tmTimelabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.tmTimelabel.backgroundColor = [UIColor clearColor];
    self.tmTimelabel.textColor = [UIColor grayColor];
    self.tmTimelabel.textAlignment = NSTextAlignmentRight;
    self.tmTimelabel.font = systemFont(13);
    [self.tmTextView addSubview:self.tmTimelabel];
    self.tmTimelabel.text = @"12:00";
    
    //小的转发消息
    self.tmlittleTextLabel = [[UILabel alloc] init];
    self.tmlittleTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
//    self.tmlittleTextLabel.backgroundColor = [UIColor redColor];
//    self.tmlittleTextLabel.textColor = [UIColor grayColor];
    self.tmlittleTextLabel.numberOfLines = 0;
    self.tmlittleTextLabel.font = systemFont(13);
    [self.tmTextView addSubview:self.tmlittleTextLabel];
    self.tmlittleTextLabel.text = @"转发过的小内容";
    
    [self _setupTransmitTextBubbleConstraints];
}

- (void)updateTransmitTextMargin:(UIEdgeInsets)margin {
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    [self _setupTransmitTextBubbleMarginConstraints];
}
@end
